package org.wanho.txy.ems.dao.impl;

import org.wanho.jdbc.JdbcTemplate;
import org.wanho.txy.ems.constant.UserConstant;
import org.wanho.txy.ems.dao.UserDao;
import org.wanho.txy.ems.entity.User;
import org.wanho.txy.ems.mapper.CountMapper;
import org.wanho.txy.ems.mapper.UserMapper;
import org.wanho.txy.ems.param.UserParam;
import org.wanho.txy.ems.util.StringUtil;
import org.wanho.util.RowMapper;

import java.util.ArrayList;
import java.util.List;


/**
 * Author：汤小洋
 * Date：2023-05-10 15:29
 * Description：<描述>
 */
public class UserDaoImpl implements UserDao {

    private JdbcTemplate<User> jt = new JdbcTemplate<>();
    private RowMapper<User> rm = new UserMapper();

    @Override
    public User selectUser(String username, String password) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   * ")
                .append(" from ")
                .append("   t_user ")
                .append(" where ")
                .append("   username=? and password=? and status=? ")
                .toString();
        return jt.queryForObject(sql,rm, username,password, UserConstant.STATUS_ENABLE);
    }

    @Override
    public void insert(User user) {
        String sql = new StringBuffer()
                .append(" insert into t_user ")
                .append("   (username,password,name,age,status,regist_date,avatar) ")
                .append(" values ")
                .append("   (?,?,?,?,?,?,?) ")
                .toString();
        jt.update(sql,user.getUsername(),user.getPassword(),user.getName(),user.getAge(),user.getStatus(),user.getRegistDate(),user.getAvatar());
    }

    @Override
    public List<User> selectByCondition(UserParam userParam, int pageNo, int pageSize) {
        StringBuffer sql = new StringBuffer()
                .append(" select ")
                .append("   * ")
                .append(" from ")
                .append("   t_user ")
                .append(" where 1=1");
        List<Object> params = new ArrayList<>();
        if(!StringUtil.isEmpty(userParam.getUsername())){
            sql.append(" and username=? ");
            params.add(userParam.getUsername());
        }
        if(!StringUtil.isEmpty(userParam.getName())){
            sql.append(" and name like ? ");
            params.add("%" + userParam.getName() + "%");
        }
        if(userParam.getStatus()!=-1){
            sql.append(" and status=? ");
            params.add(userParam.getStatus());
        }
        sql.append(" limit ?,? ");
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);
        return jt.query(sql.toString(),rm,params.toArray());
    }

    @Override
    public int selectCount(UserParam userParam) {
        StringBuffer sql = new StringBuffer()
                .append(" select ")
                .append("   count(*) ")
                .append(" from ")
                .append("   t_user ")
                .append(" where 1=1");
        List<Object> params = new ArrayList<>();
        if(!StringUtil.isEmpty(userParam.getUsername())){
            sql.append(" and username=? ");
            params.add(userParam.getUsername());
        }
        if(!StringUtil.isEmpty(userParam.getName())){
            sql.append(" and name like ? ");
            params.add("%" + userParam.getName() + "%");
        }
        if(userParam.getStatus()!=null && userParam.getStatus()!=-1){
            sql.append(" and status=? ");
            params.add(userParam.getStatus());
        }
        JdbcTemplate<Integer> jt = new JdbcTemplate<>();
        return jt.queryForObject(sql.toString(),new CountMapper(),params.toArray());
    }

    @Override
    public void updateStatus(int id, int status) {
        String sql = new StringBuffer()
                .append(" update ")
                .append("   t_user ")
                .append(" set ")
                .append("   status=? ")
                .append(" where ")
                .append("   id=? ")
                .toString();
        jt.update(sql,status,id);
    }

    @Override
    public void deleteById(int id) {
        String sql = new StringBuffer()
                .append(" delete from ")
                .append("   t_user ")
                .append(" where ")
                .append("   id=? ")
                .toString();
        jt.update(sql,id);
    }

    @Override
    public User selectById(int id) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   * ")
                .append(" from ")
                .append("   t_user ")
                .append(" where ")
                .append("   id=? ")
                .toString();
        return jt.queryForObject(sql,rm, id);
    }

    @Override
    public User selectByUsername(String username) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   * ")
                .append(" from ")
                .append("   t_user ")
                .append(" where ")
                .append("   username=? ")
                .toString();
        return jt.queryForObject(sql,rm, username);
    }

    @Override
    public void update(User user) {
        String sql = new StringBuffer()
                .append(" update ")
                .append("   t_user ")
                .append(" set ")
                .append("   username=?,password=?,name=?,age=? ")
                .append(" where ")
                .append("   id=? ")
                .toString();
        jt.update(sql,user.getUsername(),user.getPassword(),user.getName(),user.getAge(),user.getId());
    }

}
